/**
 * 
 */
package cn.hbu.zlfw.patent.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.FileHandingUtil;
import cn.hbu.zlfw.common.util.Utility;

/**
 * @ClassName: FileUploadController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author tiddar
 * @date 2018年10月14日
 *
 */
@Controller("fileUploadController")
@RequestMapping("/file")
public class FileController {
	/**
	 * 日志
	 */
	public final Logger logger = LoggerFactory.getLogger(FileController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;

	@ResponseBody
	@RequestMapping(value = "/upload.action", method = RequestMethod.POST)
	public Map<String, Object> upload(@RequestParam(value = "imgs") MultipartFile[] files) {
		String[] fileNames = FileHandingUtil.uploadFileList(files, request);
		Map<String, Object> map = Utility.getMapInstance();
		map.put("errno", 0);
		String[] linkUrls = new String[fileNames.length];
		for(int i = 0;i < linkUrls.length;i ++) {
			linkUrls[i] = request.getServletContext().getContextPath()+"/file/downloadFile.action?filename="+fileNames[i];
		}
		map.put("data", linkUrls);
		return map;
	}

	/**
	 * 
	 * @Title: downloadFile @Description: 下载附件 @return String @throws
	 */
	@ResponseBody
	@OperatorLog(type = OperatorType.OTHER, description = "下载附件")
	@RequestMapping("/downloadFile.action")
	public String downloadFile(@RequestParam(value = "filename", required = false) String filename) {
		FileHandingUtil.downloadFile(filename, response, request);
		return null;
	}

}
